package com.fbli.demo.service.impl;

import com.fbli.demo.common.msg.DemoMsg;
import com.fbli.demo.common.msg.DemoUpdateMsg;
import com.fbli.demo.entity.Demo;
import com.fbli.demo.mapper.DemoMapper;
import com.fbli.demo.service.DemoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fb_li
 * @since 2021-12-07
 */
@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, Demo> implements DemoService {

    @Override
    public boolean add(DemoMsg demoMsg) {
        Demo demo = new Demo();
        demo.setValue(demoMsg.getValue());
        demo.setMetrics(demoMsg.getMetrics());
        Date dNow = new Date( );
        long time = dNow.getTime();
        Timestamp timestamp = new Timestamp(time);
        demo.setTime(timestamp);
        try {
            this.save(demo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean insert(DemoUpdateMsg demoUpdateMsg) {
        Demo demo = new Demo();
        Integer id = demoUpdateMsg.getId();
        Demo byId = this.getById(id);
        if (byId==null){
            return false;
        }else {
            demo.setValue(demoUpdateMsg.getValue());
            demo.setMetrics(demoUpdateMsg.getMetrics());
            Date dNow = new Date( );
            long time = dNow.getTime();
            Timestamp timestamp = new Timestamp(time);
            demo.setTime(timestamp);
            demo.setId(id);
            boolean b = this.saveOrUpdate(demo);
            return b;
        }
    }
}
